package br.com.dev.tcc.view.dialog;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.BaseItem;

public class SimpleListDialog extends Dialog {

	private List<BaseItem> itens;

	public SimpleListDialog(Context context, List<BaseItem> itens) {

		super(context, R.style.DialogAppTheme);

		super.getWindow();
		super.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.setContentView(R.layout.simple_list_dialog);
		super.setCanceledOnTouchOutside(true);
		this.itens = itens;

		buildList();
	}

	public SimpleListDialog(Context context, List<BaseItem> itens,
			int iconRes, int textRes) {

		this(context, itens);
		((ImageView) findViewById(R.id.simple_list_icon))
				.setImageResource(iconRes);
		((TextView) findViewById(R.id.simple_list_icon)).setText(textRes);
	}

	private void buildList() {

		ListView list = (ListView) findViewById(R.id.simple_list_listview);

		list.setAdapter(new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				TextView tv = new TextView(getContext());

				tv.setPadding(18, 10, 18, 10);
				tv.setText(itens.get(position).getText());
				tv.setTextColor(Color.BLACK);
				tv.setTextSize(17);

				return tv;
			}

			@Override
			public long getItemId(int position) {

				return position;
			}

			@Override
			public Object getItem(int position) {

				return itens.get(position);
			}

			@Override
			public int getCount() {

				return itens.size();
			}
		});

		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> av, View v, int pos, long id) {

				if (itens.get(pos).hasIntent())
					getContext().startActivity(itens.get(pos).getIntent());

				dismiss();
			}
		});
	}

	public void show() {

		super.show();
	}
}
